package gui;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import net.proteanit.sql.DbUtils;
import codes.DBBean;

@SuppressWarnings("serial")

public class ListEmptyCategory extends javax.swing.JFrame {

	 public ListEmptyCategory() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
	        initComponents();
	    }

	                           
	    private void initComponents() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

	        returnButton = new javax.swing.JButton();
	        jLabel1 = new javax.swing.JLabel();
	        jScrollPane1 = new javax.swing.JScrollPane();
	        jTable1 = new javax.swing.JTable();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        returnButton.setText("Return");
	        returnButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                returnButtonActionPerformed(evt);
	            }
	        });

	        jLabel1.setText("List all empty leaf categories in which no web pages ");

	        jTable1.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [][] {
	                {null, null, null, null},
	                {null, null, null, null},
	                {null, null, null, null},
	                {null, null, null, null}
	            },
	            new String [] {
	                "URL", "Title", "Description", "Keywords"
	            }
	        ));
	        jScrollPane1.setViewportView(jTable1);

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(70, 70, 70)
	                        .addComponent(jLabel1)
	                        .addGap(0, 0, Short.MAX_VALUE))
	                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addGap(0, 12, Short.MAX_VALUE)
	                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                .addContainerGap())
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                .addGap(0, 0, Short.MAX_VALUE)
	                .addComponent(returnButton, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(77, 77, 77))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(27, 27, 27)
	                .addComponent(jLabel1)
	                .addGap(26, 26, 26)
	                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(29, 29, 29)
	                .addComponent(returnButton)
	                .addContainerGap(23, Short.MAX_VALUE))
	        );

	        pack();
	        
	        DBBean db = new DBBean();
			Connection con = db.openConnection();

			String query = "SELECT * FROM Category WHERE idCategory NOT IN (SELECT DISTINCT idParent from Category where idParent is NOT NULL)";

			PreparedStatement statement = con.prepareStatement(query);

			ResultSet result = statement.executeQuery();

			jTable1.setModel(DbUtils.resultSetToTableModel(result));
	    }// </editor-fold>                        

	    private void returnButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
	       new Menu().setVisible(true);
	       ListEmptyCategory.this.dispose();
	    }                                        

	    
	    public static void main(String args[]) {
	       
	        try {
	            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
	                if ("Nimbus".equals(info.getName())) {
	                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
	                    break;
	                }
	            }
	        } catch (ClassNotFoundException ex) {
	            java.util.logging.Logger.getLogger(ListEmptyCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (InstantiationException ex) {
	            java.util.logging.Logger.getLogger(ListEmptyCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (IllegalAccessException ex) {
	            java.util.logging.Logger.getLogger(ListEmptyCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
	            java.util.logging.Logger.getLogger(ListEmptyCategory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        }
	        //</editor-fold>

	        /* Create and display the form */
	        java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	                try {
						new ListEmptyCategory().setVisible(true);
					} catch (InstantiationException | IllegalAccessException
							| ClassNotFoundException | SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }
	        });
	    }

	    // Variables declaration - do not modify                     
	    private javax.swing.JButton returnButton;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JTable jTable1;
	    // End of variables declaration                   
	}
